﻿using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Channels;

namespace Framework.Remoting.Conventions
{
    public class ProtocolFactory : IProtocolFactory
    {
        private readonly Dictionary<Type, string> protocols = new Dictionary<Type, string>();

        public ProtocolFactory()
        {
            AddProtocol<NetTcpBinding>("net.tcp");
            AddProtocol<BasicHttpBinding>("http");
        }

        
        public string Create(Binding binding)
        {
            var bindingType = binding.GetType();

            if (!protocols.ContainsKey(bindingType))
            {
                throw new ProtocolNotFoundException("Protocol for Binding {0} does not exist");
            }

            return protocols[bindingType];
        }

        

        private void AddProtocol<TBinding>(string protocol)
            where TBinding : Binding
        {
            protocols.Add(typeof (TBinding), protocol);
        }
    }
}